﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using HighDivePrinter.Model;
using HighDivePrinter.Properties;
using System.Windows.Forms;

namespace HighDivePrinter 
{
    public class PrintDocumentContest : System.Drawing.Printing.PrintDocument
    {
        /// <summary>
        /// Variabel för att hålla reda på vilken sida jag skriver ut på.
        /// </summary>
        private int _printPageCounter;
        private BindingList<Contest> _listContest = null;
        private BindingList<DiveScore> _listScore = null;
        public PrintDocumentContest(BindingList<Contest> listContest)
            : base()
        {

            _listContest = listContest;
        }


        public void MyPrint()
        {
            _printPageCounter = 0;

            //System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
            System.Windows.Forms.PrintPreviewDialog dialog = new PrintPreviewDialog();


           this.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintOnePage);

            dialog.Document = this;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Print(); // Här sker utskriften, fyrar av bl.a EVENT PrintPage
            }
        }

          /// <summary>
        /// Metod för att svara på dokumenthändelsen EVENT PrintPage för att skriva ut nästa sid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ev"></param>
        private void PrintOnePage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        {
            Font font = new Font(FontFamily.GenericMonospace, 12);
            Font printFont = font;
            SolidBrush myBrush = new SolidBrush(Color.Black);

            int linesPerPage = (int)(ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics));
            float yPosition = 0;
            int row = 3;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;
            string line2 = null;
            
           // Pen p = new Pen(Color.Red, 4);
          //  Rectangle r = new Rectangle(0, 0, ev.MarginBounds.Width, ev.MarginBounds.Height);
            //ev.Graphics.DrawEllipse(p, r);
            //ev.Graphics.DrawRectangle(p, r);

            Bitmap bmp = Properties.Resources.Contest;
            Image newImage = bmp;
            ev.Graphics.DrawImage(newImage, 0, 0, 850, 150);

            if (_listContest.Count != 0)
            {
                row = 4;
                yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
                ev.Graphics.DrawString("Name", printFont, myBrush, (leftMargin - 60), yPosition, new StringFormat());
                ev.Graphics.DrawString("Club", printFont, myBrush, (leftMargin + 69), yPosition, new StringFormat());
                ev.Graphics.DrawString("City", printFont, myBrush, (leftMargin + 200), yPosition, new StringFormat());
                ev.Graphics.DrawString("Country", printFont, myBrush, (leftMargin + 310), yPosition, new StringFormat());
                ev.Graphics.DrawString("Status", printFont, myBrush, (leftMargin + 470), yPosition, new StringFormat());
                ev.Graphics.DrawString("Date", printFont, myBrush, (leftMargin + 630), yPosition, new StringFormat());
                

                row = 7;
                while (row < (linesPerPage - 7))
                {
                    line = null;
                    int i = _printPageCounter * (linesPerPage - 10) + (row - 7);
                    if (i >= _listContest.Count)
                        break;

                    Contest Contest = _listContest[i];
                    line =Contest.Name +"     " + Contest.Club + "       " + Contest.City + "      " + Contest.Country  + "       " + Contest.Status+ "      " + Contest.Date;

             
                    yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
                    ev.Graphics.DrawString(line, printFont, myBrush, (leftMargin - 60), yPosition, new StringFormat());

                    row++;

                }
            }

            row++;
            yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
            ev.Graphics.DrawString(_printPageCounter.ToString(), printFont, myBrush, leftMargin, yPosition, new StringFormat());

            // If there are more lines, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
            myBrush.Dispose();

            this._printPageCounter++;
        } 

    
       /* public PrintDocumentContest(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }*/
    }
}

